home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / psion / amigancp.lha / AmigaNCP010b / Developer / Source / NCPMon / ncpmon.c < prev    next >
C/C++ Source or Header  |  1994-10-28  |  7KB  |  362 lines

  1. /*
  2. ** AmigaNCP Monitor
  3. ** ----------------
  4. **
  5. ** (C) 1993-1994 Oliver Wagner, All Rights Reserved
  6. **
  7. ** Small'n'ugly hack to monitor NCP activity.
  8. **
  9. */
  10.  
  11. #include <proto/dos.h>
  12. #define __USE_SYSBASE
  13. #include <proto/icon.h>
  14. #include <proto/exec.h>
  15. #include <proto/intuition.h>
  16. #include <proto/graphics.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20.  
  21. #include <libraries/ncplib.h>
  22.  
  23.  
  24. /*
  25. ** Private data structures of amigancp.library
  26. */
  27.  
  28. struct NCP_Message
  29. {
  30.     struct Message m;
  31.     short cmd;
  32.     USHORT datasize;
  33.     void *data;
  34.     struct NCP_Channel *ncp_channel;
  35. };
  36.  
  37. struct NCP_Channel
  38. {
  39.     ULONG readsigmask,            /* Signalled when new PKT arrived (Mask!) */
  40.      writesigmask;                /* Signalled when PKT send (Mask!) */
  41.  
  42.     ULONG flags;
  43.  
  44.     /*** the following is private!!! ***/
  45.  
  46.     USHORT thischannel,            /* Channel allocation */
  47.      thatchannel;
  48.  
  49.     char thisname[12], thatname[12];    /* "Process" names */
  50.  
  51.     struct List incoming;        /* incoming PKTs */
  52.     struct List readbuffer;        /* incoming segments */
  53.  
  54.     struct MsgPort *readport, *writeport;
  55.     struct NCP_Message readmsg, writemsg;
  56.  
  57.     ULONG bytesread, byteswritten;
  58. };
  59.  
  60. #define NCPCF_ALLOC 1            /* Channel is allocated */
  61. #define NCPCF_ACTIVE 2            /* Connections exists */
  62. #define NCPCF_CONNREQ 4            /* Connection request underway */
  63. #define NCPCF_REMOTECLOSED 8    /* Closed by remote */
  64. #define NCPCF_OFFLINE    16        /* LLMAC is offline */
  65. #define NCPCF_XOFF        32        /* Data transmission held */
  66.  
  67. struct NCP_Info
  68. {
  69.     ULONG rt, mt;
  70.     UWORD remver;
  71.     UWORD conn;
  72.     ULONG skb, rkb;
  73. };
  74.  
  75. char version[]=
  76. {"$VER: AmigaNCP-Monitor 1.4 (28.10.94)"};
  77.  
  78. static struct NCP_Channel *ncp;
  79. static struct Window *w;
  80. static struct RastPort *rp;
  81. static struct TextAttr top80 =
  82. {"topaz.font", 8, 0, 0};
  83. static struct MsgPort *infoport;
  84. static struct timerequest *treq;
  85. static struct NCP_Info ninfo;
  86. static struct DiskObject *diskobj;
  87.  
  88. extern struct WBStartup *_WBenchMsg;
  89.  
  90. /*** Requires 2.04 ++ ***/
  91. ULONG __oslibversion = 37;
  92.  
  93. /*** SAS/C cback.o startup */
  94. char *__procname = "NCP Monitor";
  95. ULONG __priority = -1;
  96. ULONG __stack = 4000;
  97. ULONG _BackGroundIO = FALSE;
  98.  
  99. #define text(x,y,s) Move(rp,x,y);Text(rp,s,strlen(s))
  100.  
  101. static void __stdargs 
  102. sprintf(char *to, char *fmt,...)
  103. {
  104.     static UWORD fmtfunc[]=
  105.     {á0x16c0, 0x4e75};
  106.  
  107.     RawDoFmt(fmt, &fmt + 1, (APTR) fmtfunc, to);
  108. }
  109.  
  110. void
  111. cleanup(void)
  112. {
  113.     if (diskobj)
  114.         FreeDiskObject(diskobj);
  115.     if (treq)
  116.     {
  117.         if (treq->tr_node.io_Device)
  118.             CloseDevice(treq);
  119.         DeleteIORequest(treq);
  120.     }
  121.     if (infoport)
  122.         DeletePort(infoport);
  123.     if (w)
  124.         CloseWindow(w);
  125. }
  126.  
  127. static void
  128. drawncp(int y, struct NCP_Channel *ncp)
  129. {
  130.     char buffer[80];
  131.  
  132.     sprintf(buffer, "%ld %-8.8s  %ld %-8.8s  %lc%lc%lc%lc%lc%lc  %10ld %10ld",
  133.             ncp->thischannel, ncp->thisname,
  134.             ncp->thatchannel, ncp->thatname,
  135.             (ncp->flags & NCPCF_ALLOC) ? 'A' : '-',
  136.             (ncp->flags & NCPCF_ACTIVE) ? 'A' : '-',
  137.             (ncp->flags & NCPCF_CONNREQ) ? 'C' : '-',
  138.             (ncp->flags & NCPCF_REMOTECLOSED) ? 'R' : '-',
  139.             (ncp->flags & NCPCF_OFFLINE) ? 'O' : '-',
  140.             (ncp->flags & NCPCF_XOFF) ? 'X' : '-',
  141.             ncp->byteswritten, ncp->bytesread);
  142.  
  143.     text(4, y, buffer);
  144. }
  145.  
  146. static char *
  147. datstr(time_t t)
  148. {
  149.     struct tm *tm = localtime(&t);
  150.     static char dat[64];
  151.  
  152.     if (!t)
  153.         return ("--            ");
  154.  
  155.     sprintf(dat, "%02ld:%02ld:%02ld %02ld-%02ld",
  156.             tm->tm_hour, tm->tm_min, tm->tm_sec,
  157.             tm->tm_mday, tm->tm_mon + 1);
  158.  
  159.     return (dat);
  160. }
  161.  
  162.  
  163. static void
  164. drawninfo(void)
  165. {
  166.     char buffer[80];
  167.  
  168.     NCP_private1(&ninfo);
  169.  
  170.     strcpy(buffer, datstr(ninfo.mt));
  171.     text(116, 79, buffer);
  172.  
  173.     strcpy(buffer, datstr(ninfo.rt));
  174.     text(116, 87, buffer);
  175.  
  176.     sprintf(buffer, "%ld  %9.9s", ninfo.remver, ninfo.conn ? "[Connect]" : NULL);
  177.     text(332, 87, buffer);
  178.  
  179.     sprintf(buffer, "%10ld %10ld", ninfo.skb, ninfo.rkb);
  180.     text(260, 79, buffer);
  181.  
  182. }
  183.  
  184. static void
  185. drawinfo(void)
  186. {
  187.     SetDrMd(rp, JAM2);
  188.     SetAPen(rp, 3);
  189.     RectFill(rp, 0, 0, 432, 90);
  190.  
  191.     SetBPen(rp, 3);
  192.     SetAPen(rp, 2);
  193.  
  194.     /* Infoline */
  195.     text(4, 8, "# ThisProc  # RemotePr  Status  Bytes Sent  Received");
  196.     text(4, 79, "Online since:                 =");
  197.     text(4, 87, "Remote NCP..:                 Version:");
  198.     Move(rp, 0, 10);
  199.     Draw(rp, 431, 10);
  200.     Move(rp, 0, 70);
  201.     Draw(rp, 431, 70);
  202.  
  203.     SetAPen(rp, 1);
  204. }
  205.  
  206. void __tzset( void )
  207. {
  208.     __timezone = 0;
  209. }
  210.  
  211.  
  212. int
  213. main(int argc, char **argv)
  214. {
  215.     struct IntuiMessage *im, imsg;
  216.     int c;
  217.     ULONG sigs, infosig, idcmpsig;
  218.     char windowtitle[ 128 ];
  219.     USHORT windowx = 0, windowy = 0;
  220.     char *iconname;
  221.  
  222.     /* Are we already running? */
  223.     Forbid();
  224.     infoport = FindPort("NCP Monitor");
  225.     if (infoport)
  226.     {
  227.         Signal(infoport->mp_SigTask, SIGBREAKF_CTRL_F);
  228.         Permit();
  229.         return (0);
  230.     }
  231.     infoport = CreatePort("NCP Monitor", -128);
  232.     infosig = 1 << infoport->mp_SigBit;
  233.     Permit();
  234.  
  235.     onexit(cleanup);
  236.  
  237.     /*** Load Icon ***/
  238.     if (argc)
  239.         diskobj = GetDiskObjectNew(iconname = argv[0] á);
  240.     else
  241.         diskobj = GetDiskObjectNew(iconname = _WBenchMsg->sm_ArgList[0].wa_Name);
  242.  
  243.     if (diskobj)
  244.     {
  245.         char *def = FindToolType(diskobj->do_ToolTypes, "WINDOW");
  246.  
  247.         if (def)
  248.         {
  249.             windowx = atoi(def);
  250.             def = strchr(def, '/');
  251.             if (def++)
  252.             {
  253.                 windowy = atoi(def);
  254.             }
  255.         }
  256.     }
  257.  
  258.     sprintf(windowtitle, "AmigaNCP Monitor - amigancp.library v%ld.%ld [%s]",
  259.             NCPBase->lib_Version,
  260.             NCPBase->lib_Revision,
  261.             NCPBase->lib_IdString
  262.         );
  263.  
  264.     /*** Open Window ***/
  265.     w = OpenWindowTags(NULL,
  266.                        (windowx) ? WA_Left : TAG_IGNORE, windowx,
  267.                        (windowy) ? WA_Top : TAG_IGNORE, windowy,
  268.                        WA_InnerWidth, 432,
  269.                        WA_InnerHeight, 90,
  270.                        WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  271.                        WA_Flags, WINDOWDRAG | WINDOWDEPTH | RMBTRAP | GIMMEZEROZERO | WINDOWCLOSE | SIMPLE_REFRESH | WINDOWSIZING,
  272.                        WA_MinWidth, 120,
  273.                        WA_MinHeight, 25,
  274.                        WA_Title, windowtitle,
  275.                        WA_ScreenTitle, &version[ 6 ],
  276.                        WA_AutoAdjust, TRUE,
  277.                        TAG_DONE
  278.         );
  279.  
  280.     if (!w)
  281.     {
  282.         PutStr("can't open window");
  283.         return (20);
  284.     }
  285.  
  286.     treq = CreateIORequest( infoport, sizeof( *treq ) );
  287.     OpenDevice( "timer.device", UNIT_VBLANK, treq, 0 );
  288.  
  289.     idcmpsig = 1 << w->UserPort->mp_SigBit;
  290.  
  291.     rp = w->RPort;
  292.     SetFont(rp, OpenFont(&top80));
  293.  
  294.     drawinfo();
  295.  
  296.     ncp = NCP_private1(NULL);
  297.  
  298.     /*** Main Loop ***/
  299.     FOREVER
  300.     {
  301.         SetSignal(0, infosig);
  302.  
  303.         for (c = 0; c < 7; c++)
  304.             drawncp(19 + c * 8, &ncp[c]);
  305.  
  306.         drawninfo();
  307.  
  308.         treq->tr_node.io_Command = TR_ADDREQUEST;
  309.         treq->tr_time.tv_secs = ( w->Flags & WFLG_WINDOWACTIVE ) ? 1 : 3;
  310.         treq->tr_time.tv_micro = 0;
  311.  
  312.         SendIO(treq);
  313.  
  314.         sigs = Wait(infosig | idcmpsig | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F);
  315.  
  316.         /* Abort Timer */
  317.         AbortIO(treq);
  318.         WaitIO(treq);
  319.  
  320.         im = (struct IntuiMessage *) GetMsg(w->UserPort);
  321.         if (im)
  322.         {
  323.             imsg = *im;
  324.             ReplyMsg(im);
  325.             if (imsg.Class == CLOSEWINDOW)
  326.                 break;
  327.             else if (imsg.Class == REFRESHWINDOW)
  328.             {
  329.                 BeginRefresh(w);
  330.                 drawinfo();
  331.                 EndRefresh(w, TRUE);
  332.             }
  333.         }
  334.  
  335.         if (sigs & SIGBREAKF_CTRL_C)
  336.             break;
  337.  
  338.         if (sigs & SIGBREAKF_CTRL_F)
  339.             WindowToFront(w);
  340.     }
  341.  
  342.     /*** Save Icon ***/
  343.     if (diskobj)
  344.     {
  345.         char *tt[2];
  346.  
  347.         sprintf(windowtitle, "WINDOW=%ld/%ld",
  348.                 w->LeftEdge,
  349.                 w->TopEdge
  350.             );
  351.  
  352.         tt[0] = windowtitle;
  353.         tt[1] = NULL;
  354.  
  355.         diskobj->do_ToolTypes = tt;
  356.         PutDiskObject(iconname, diskobj);
  357.     }
  358.  
  359.     /*** Exit ***/
  360.     return (0);
  361. }
  362.